# -*- coding: utf-8 -*-
# @Time : 2022/2/7 11:46

from Common.mysql import db
from Common.public_method import utc_8
from src.ApiTest.ProjectConfig.Database.baseInfo_database import BaseInfoConfig
from sqlalchemy.exc import SQLAlchemyError
from Common.yaml_method import YamlMethod


class UpdateBaseInfo:
    """
    更新信息接口
    """

    @staticmethod
    def update_base_info(system_id, system_name, project_id, project_name, base_url_test, base_url_develop, base_url_release, header, update_user):
        """
        更新配置信息
        :param system_id: 系统ID
        :param system_name: 配置名称
        :param project_id: 项目ID
        :param project_name: 项目名称
        :param base_url_test: 测试环境base URL
        :param base_url_develop: 开发环境base url
        :param base_url_release: 正式环境base url
        :param header: 请求头
        :param update_user: 更新人
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        update_time = utc_8()

        info = BaseInfoConfig.query.filter_by(id=system_id).first()

        if info:
            info.systemName = system_name
            info.projectId = project_id
            info.projectName = project_name
            info.base_url_test = base_url_test
            info.base_url_develop = base_url_develop
            info.base_url_release = base_url_release
            info.header = header
            info.update_time = update_time
            info.update_user = update_user

            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
                res = {
                    'code': code[6],
                    'data': [],
                    'message': '信息更新失败'
                }
                return res

            res = {
                'code': code[0],
                'data': [],
                'message': '信息更新成功'
            }
            return res
        else:
            res = {
                'code': code[3],
                'data': [],
                'message': '信息不存在'
            }
            return res
